package com.course.db;

import java.sql.ResultSet;

public class Student extends ExecuteDB  {

	//members
	private int Id;
	private String name;
	private String no;
	private String grade;
	private String strclass;
	private int user_id;
	private String strSql;
	
	public Student()
	{
		this.Id = 0;
		this.name = "";
		this.no = "";
		this.grade = "";
		this.strclass = "";
		this.user_id = 0;
	}
	
	public String CountSql()
	{
		this.strSql = "select count(*) from student";
		return this.strSql;
	} 
	
	//add a student
	public boolean add()
	{

		this.strSql="insert into student ";
		this.strSql=this.strSql + "(no,name,grade,strclass,user_id) ";
		this.strSql=this.strSql + "values('" + this.no + "','" + this.name+ "','" + this.grade + "','" + this.strclass+ "','" + this.user_id + "')";
		System.out.println(this.strSql);
		boolean isAdd = super.exeSql(this.strSql);
        return isAdd;
	}
	
	public int lastid()
	{
		int lid = 0;
		this.strSql = "select last_insert_id() as id from student limit 1";
		ResultSet rs = null;
		try
		{
			rs = super.exeQuery(this.strSql);
			if (rs.next())
				lid = rs.getInt("id");
		}
		catch(Exception e)
		{
			System.out.println(e.toString());
		}
		return lid;
	}
	
	//delete a student by id
    public boolean delete(int StudentID)
    {
       this.strSql = "delete from user where Id in (";
       this.strSql = this.strSql + StudentID +")";

       boolean isDelete = super.exeSql(this.strSql);
       return isDelete;
    }
    
  //initialize student information
 	public boolean init()
	{
		this.strSql="select * from `student` where Id=";
		this.strSql=this.strSql + "'" + this.Id + "'";

		try
		{
			ResultSet rs = super.exeQuery(this.strSql);
			if (rs.next())
			{
				this.Id=rs.getInt("Id");
				this.name=rs.getString("name");
				this.no=rs.getString("no");
				this.strclass=rs.getString("class");
				this.grade=rs.getString("grade");
				this.user_id=rs.getInt("user_id");
				return true;
			}
			else
			{
				return false;
			}
		}
		catch(Exception e)
		{
			System.out.println(e.toString());
			return false;
		}
	}
 	
 	//modify student infomation
    public boolean modify_student()
	{
		this.strSql="update student set";
		this.strSql=this.strSql + " name=" + "'" + this.name + "',";
		this.strSql=this.strSql + " no=" + "'" + this.no + "',";
		this.strSql=this.strSql + " class=" + this.strclass + ",";
		this.strSql=this.strSql + " grade=" + this.grade + ",";
		this.strSql=this.strSql + " user_id=" + this.user_id;
		this.strSql=this.strSql + " where Id=" + this.Id;
		
		boolean isUpdate = super.exeSql(this.strSql);

		return isUpdate;
	}
    
  //show students
    public ResultSet show_students()
	{
		this.strSql="select * from `student`";
		ResultSet rs = null;
		try
		{
			rs = super.exeQuery(this.strSql);
		}
		catch(Exception e)
		{
			System.out.println(e.toString());
		}
		return rs;
	}

	public int getId() {
		return Id;
	}

	public void setId(int id) {
		Id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getNo() {
		return no;
	}

	public void setNo(String no) {
		this.no = no;
	}

	public String getGrade() {
		return grade;
	}

	public void setGrade(String grade) {
		this.grade = grade;
	}

	public String getStrclass() {
		return strclass;
	}

	public void setStrclass(String strclass) {
		this.strclass = strclass;
	}

	public int getUser_id() {
		return user_id;
	}

	public void setUser_id(int user_id) {
		this.user_id = user_id;
	}

	public String getStrSql() {
		return strSql;
	}

	public void setStrSql(String strSql) {
		this.strSql = strSql;
	}
 	
    
}
